package com.ibeeking.found.common.config.feign.fallback;

import com.ibeeking.found.common.entity.*;
import com.ibeeking.found.common.entity.open.OpenAuthQuery;
import com.ibeeking.found.common.entity.open.OpenConfigDTO;
import com.ibeeking.found.common.entity.open.OpenConfigQuery;
import com.ibeeking.found.common.config.feign.GlobalClient;
import com.ibeeking.found.common.entity.*;
import com.ibeeking.found.common.entity.open.OpenAuthDTO;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 降级工场
 *
 * @Author ibeeking
 * @date 2019/1/18
 */
@Component
public class GlobalClientFallbackFactory implements FallbackFactory<GlobalClient> {
    @Override
    public GlobalClient create(Throwable throwable) {
        return new GlobalClient() {

            @Override
            public ResponseResult<List<IndustryFrontendDTO>> queryIndustryFrontend(String industry, String frontend) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，行业的前端可见性失败。" + throwable.getMessage(), Collections3.emptyList());
            }

            @Override
            public ResponseResult<GlobalVariableDTO> queryGlobalVariable(VariableQuery query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询全局变量失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<TenantVariableDTO> queryTenantVariable(VariableQuery query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询租户全局变量失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<List<OpenConfigDTO>> queryOpenConfigList(OpenConfigQuery query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询开放平台授权配置失败。" + throwable.getMessage(), Collections3.emptyList());
            }

            @Override
            public ResponseResult<OpenConfigDTO> queryOpenConfigOne(OpenConfigQuery query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询开放平台授权配置失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<List<OpenAuthDTO>> queryOpenAuthList(OpenAuthQuery query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询开放平台授权失败。" + throwable.getMessage(), Collections3.emptyList());
            }

            @Override
            public ResponseResult<OpenAuthDTO> queryOpenAuthOne(OpenAuthQuery query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询开放平台授权失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<List<Integer>> queryTenantIds() {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询有效租户ID失败。" + throwable.getMessage(), Collections3.emptyList());
            }

            @Override
            public ResponseResult<DatasourceConfigDTO> queryDatasourceConfig(Long configId) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询数据源配置失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<DatasourceConfigDTO> queryTenantDatasource(Integer tenantId) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，通过租户编码查询租户数据源失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<List<DatabaseTemplateDTO>> listTemplateByIndustry(String industry) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询模板库失败。", Collections.emptyList());
            }

            @Override
            public ResponseResult<TenantDTO> queryTenantByCode(String code) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，通过租户编码查询有效租户ID失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<TenantDTO> queryTenantById(Integer id) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，通过租户Id查询有效租户失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<String> queryTenantIndustryById(Integer id) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，通过租户Id查询有效租户Industry失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<TimeTaskDTO> queryTimeTaskByHandler(String industry, String handler) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询定时任务模板失败。" + throwable.getMessage(), null);
            }

            @Override
            public ResponseResult<List<DictDTO>> queryDictByTypes(String[] dictTypes) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，批量查询字典列表失败。", Collections3.emptyList());
            }

            @Override
            public ResponseResult<List<DictDTO>> queryDictByType(String dictType) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询字典列表失败。", Collections3.emptyList());
            }

            @Override
            public ResponseResult<List<DictDTO>> queryDictLables(Map<String, String> paramMap) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，批量查询字典标签失败。", Collections3.emptyList());
            }

            @Override
            public ResponseResult<DictDTO> queryDictLable(String dictType, String dictValue) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询字典标签失败。");
            }

            @Override
            public ResponseResult<AreaDTO> queryName(String code) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询地区信息失败。");
            }

            @Override
            public ResponseResult<List<AreaDTO>> queryNames(List<String> codes) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询地区信息失败。", Collections3.emptyList());
            }

            @Override
            public ResponseResult<List<AreaDTO>> queryArea(AreaQuery areaQuery) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询地区信息失败。", Collections3.emptyList());
            }

            @Override
            public ResponseResult<List<OperatorDTO>> queryByUserIds(List<Long> userIds) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，根据userIds查询用户信息失败。" + throwable.getMessage(), Collections3.emptyList());
            }

            @Override
            public ResponseResult<List<ShortUrlDTO>> queryShortUrlList(ShortUrlQueryFeign query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询短链接列表失败。" + throwable.getMessage(), Collections3.emptyList());
            }

            @Override
            public ResponseResult<ShortUrlDTO> queryShortUrlOne(ShortUrlQueryFeign query) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，查询短链接失败。");
            }

        };
    }
}
